package com.coco.goods.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.coco.common.constant.Constant;
import com.coco.common.result.ResultData;
import com.coco.goods.mapper.EvaluateMapper;
import com.coco.goods.mapper.GoodsMapper;
import com.coco.goods.pojo.TbGoods;
import com.coco.goods.service.EvaluateService;
import com.coco.common.util.DateUtils;
import com.coco.goods.vo.Evaluate;
import com.coco.goods.vo.EvaluateSearchVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @program: coco
 * @author: Mr.Lin
 * @create: 2021-07-15 19:10
 **/
@Service
public class EvaluateServiceImpl implements EvaluateService {

    @Autowired(required = false)
    private EvaluateMapper evaluateMapper;
    @Autowired(required = false)
    private GoodsMapper goodsMapper;
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void addGoodsEvaluate(Evaluate evaluate) {
        //根据spuId查询商品信息
        TbGoods goods = goodsMapper.findById(evaluate.getGoodsId());
        //给 evaluate 赋值
        evaluate.setSpuName(goods.getSpuName());
        evaluate.setGoodsImg(goods.getSpuImage());
        evaluate.setEvaluateTime(new Date());
        mongoTemplate.insert(evaluate, "evaluate");
    }

    /**
     * @param evaluate
     * @Author: 张dy
     * @Date: 2021/7/15
     * @Param: [evaluate]
     * @return: com.coco.common.result.ResultData
     * @Description:评论列表 模糊 + 分页
     */
    @Override
    public ResultData search(EvaluateSearchVo evaluate) {
        PageHelper.startPage(evaluate.getPage(), evaluate.getLimit());
        Query query = new Query();
        List<Evaluate> evaluates = new ArrayList<>();
        if (evaluate.getStatus()!=null) {
            query.addCriteria(Criteria.where(Constant.Evaluate.IS_STATUS).is(evaluate.getStatus()));
        }
        if (!StringUtils.isEmpty(evaluate.getSpuName())) {
            query.addCriteria(Criteria.where(Constant.Evaluate.IS_SPUNAME).is(evaluate.getSpuName()));
        }
        if (!StringUtils.isEmpty(evaluate.getUserName())) {
            query.addCriteria(Criteria.where(Constant.Evaluate.IS_USERNAME).is(evaluate.getUserName()));
        }
        if (evaluate.getDateTime() != null) {
            //前999毫秒的时间
            Date frontSecond = DateUtils.getFrontSecond(evaluate.getDateTime(), 999);
            //后999毫秒的时间
            Date nextSecond = DateUtils.getNextSecond(evaluate.getDateTime(), 999);
            System.out.println(nextSecond);
            query.addCriteria(Criteria.where("evaluateTime").gte(frontSecond).lte(nextSecond));
        }
        Integer day = evaluate.getDay();
        switch (day) {
            case Constant.Evaluate.IS_TODAY: {
                //今天开始时间
                Date dayBegin = DateUtils.getDayBegin();
                query.addCriteria(Criteria.where("evaluateTime").gte(dayBegin));
                return findEvaluates(query);
            }
            case Constant.Evaluate.IS_YESTERDAY: {
                //昨天开始时间
                Date beginDayOfYesterday = DateUtils.getBeginDayOfYesterday();
                //今天开始时间
                Date dayBegin = DateUtils.getDayBegin();
                query.addCriteria(Criteria.where("evaluateTime").gte(beginDayOfYesterday).lte(dayBegin));
                return findEvaluates(query);
            }
            case Constant.Evaluate.IS_WEEK: {
                //7天内时间
                Date frontDay = DateUtils.getFrontDay(new Date(), 7);
                query.addCriteria(Criteria.where("evaluateTime").gte(frontDay));
                return findEvaluates(query);
            }
            case Constant.Evaluate.IS_MONTH: {
                //本月开始时间
                Date beginDayOfMonth = DateUtils.getBeginDayOfMonth();
                query.addCriteria(Criteria.where("evaluateTime").gte(beginDayOfMonth));
                return findEvaluates(query);
            }
            case Constant.Evaluate.IS_THIRTY_DAYS: {
                //30天内时间
                Date frontDay = DateUtils.getFrontDay(new Date(), 30);
                query.addCriteria(Criteria.where("evaluateTime").gte(frontDay));
                return findEvaluates(query);
            }
            case Constant.Evaluate.IS_YEAR: {
                //获取今年开始时间
                Date beginDayOfYear = DateUtils.getBeginDayOfYear();
                query.addCriteria(Criteria.where("evaluateTime").gte(beginDayOfYear));
                return findEvaluates(query);
            }
            default: {
                evaluates = mongoTemplate.find(query,Evaluate.class);
                PageInfo<Evaluate> evaluatePageInfo = new PageInfo<>(evaluates);
                return new ResultData(evaluatePageInfo.getList(), evaluatePageInfo.getTotal());
            }
        }
    }

    /**
     *   查询mongodb数据   静态方法
     *
     * @Author: 张dy
     * @Date: 2021/8/6
     * @Param: [query]
     */
    private ResultData findEvaluates(Query query) {
        List<Evaluate> evaluateList = mongoTemplate.find(query, Evaluate.class);
        PageInfo<Evaluate> evaluatePageInfo = new PageInfo<>(evaluateList);
        return new ResultData(evaluatePageInfo.getList(), evaluatePageInfo.getTotal());
    }


    @Override
    public void deleteEvaluateById(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        mongoTemplate.remove(query, Evaluate.class);

    }

    @Override
    public void addReplyById(String id, String replyContent) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update =new Update().set("replyContent", replyContent);
        UpdateResult evaluate = mongoTemplate.updateMulti(query, update, "evaluate");
        System.out.println(evaluate.getModifiedCount());
    }


}